{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Linear regression python multi-dimensional data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Linear Regression with two variables in one dimensional data\n",
    "\n",
    " \n",
    " \n",
    " $$ F(X)=X \\times W $$\n",
    " $$ C=|| F(X) - Y ||_2^2 + \\lambda ||W||_2^2$$\n",
    "\n",
    "$X_{n \\times k}$\n",
    "\n",
    "$W_{k \\times p}$\n",
    "\n",
    "$Y_{n \\times p}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "n, k, p=100, 8, 3 \n",
    "X=np.random.random([n,k])\n",
    "W=np.random.random([k,p])\n",
    "Y=np.random.random([n,p])\n",
    "max_itr=1000\n",
    "alpha=0.0001\n",
    "Lambda=0.01"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Gradient is as follows:\n",
    "$$ X^T 2 E + \\lambda 2 W$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# F(x)= w[0]*x + w[1]\n",
    "def F(X, W):\n",
    "    return np.matmul(X,W)\n",
    "\n",
    "def cost(Y_est, Y, W, Lambda):\n",
    "    E=Y_est-Y\n",
    "    return E, np.linalg.norm(E,2)+ Lambda * np.linalg.norm(W,2)\n",
    "\n",
    "def gradient(E,X, W, Lambda):\n",
    "    return 2* np.matmul(X.T, E) + Lambda* 2* W"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fit(W, X, Y, alpha, Lambda, max_itr):\n",
    "    for i in range(max_itr):\n",
    "        \n",
    "        Y_est=F(X,W)\n",
    "        E, c= cost(Y_est, Y, W, Lambda)\n",
    "        Wg=gradient(E, X, W, Lambda)\n",
    "        W=W - alpha * Wg\n",
    "        if i%100==0:\n",
    "            print(c)\n",
    "        \n",
    "    return W"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To take into account for the biases, we concatenate X by a 1 column, and increase the number of rows in W by one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "34.3004759224227\n",
      "4.265835757989014\n",
      "4.052505749060854\n",
      "3.8807845759072968\n",
      "3.7422281683979812\n",
      "3.6303399157863434\n",
      "3.5398708528835554\n",
      "3.4665749938168915\n",
      "3.4070257924246747\n",
      "3.3584711183863862\n"
     ]
    }
   ],
   "source": [
    "X=np.concatenate( (X, np.ones((n,1))), axis=1 ) \n",
    "W=np.concatenate( (W, np.random.random((1,p)) ), axis=0 )\n",
    "\n",
    "W = fit(W, X, Y, alpha, Lambda, max_itr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
